package hhm.android.library.weightHeightPicker.weightPicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import hhm.android.library.datePicker.view.WheelView;
import hhm.android.library.utils.GetStringListener;

public class WeightPickerView extends LinearLayout {

    final WeightPick mDatePick = new WeightPick();
    private GetStringListener mOnChangeListener;
    private String type;
    private BaseWeightAdapter mgAdapter;
    private WheelView mMgView;

    public WeightPickerView(Context context) {
        super(context);
    }

    public WeightPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WeightPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnChangeListener(GetStringListener listener) {
        this.mOnChangeListener = listener;
    }

    public void show(String type, double initData, @NonNull WeightParams params, int selectColor) {
        this.type = type;

        setInitData(initData);

        if (params.types == null) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        for (int t : params.types) {

            WheelView wheelView = new WheelView(getContext());
            final BaseWeightAdapter datePickAdapter;

            switch (t) {
                case WeightParams.TYPE_KG:
                    wheelView.setCyclic(false);
                    datePickAdapter = new KGAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.kg = datePickAdapter.getValue(newValue);
                        if (mgAdapter != null) {
                            mgAdapter.refreshValues();
                        }
                        if (mMgView != null) {
                            mMgView.setCurrentItem(mgAdapter.getCurrentIndex());
                        }
                        WeightPickerView.this.onChanged();
                    });
                    break;
                case WeightParams.TYPE_MG:
                    mMgView = wheelView;
                    wheelView.setCyclic(true);
                    datePickAdapter = new MGAdapter(params, mDatePick, selectColor);
                    mgAdapter = datePickAdapter;
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mg = datePickAdapter.getValue(newValue);
                        WeightPickerView.this.onChanged();
                    });
                    break;
                case WeightParams.TYPE_JIN:
                    wheelView.setCyclic(false);
                    datePickAdapter = new JINAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.kg = datePickAdapter.getValue(newValue);
                        WeightPickerView.this.onChanged();
                    });
                    break;
                case WeightParams.TYPE_LIANG:
                    wheelView.setCyclic(true);
                    datePickAdapter = new LIANGAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mg = datePickAdapter.getValue(newValue);
                        WeightPickerView.this.onChanged();
                    });
                    break;
                case WeightParams.TYPE_LB:
                    wheelView.setCyclic(false);
                    datePickAdapter = new LBAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.kg = datePickAdapter.getValue(newValue);
                        WeightPickerView.this.onChanged();
                    });
                    break;
                case WeightParams.TYPE_OZ:
                    wheelView.setCyclic(true);
                    datePickAdapter = new OZAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mg = datePickAdapter.getValue(newValue);
                        WeightPickerView.this.onChanged();
                    });
                    break;
            }

            addView(wheelView, layoutParams);
        }
    }

    private void onChanged() {
        if (mOnChangeListener != null) {
            mOnChangeListener.getString(getChildHeight());
        }
    }

    public String getChildHeight() {
        int kg = mDatePick.kg;
        int mg = mDatePick.mg;
        if (type.equals("JIN")) {//斤
            if (mg < 10) {
                return kg + ".0" + mg;
            } else {
                return kg + "." + mg;
            }
        } else if (type.equals("LB")) {//磅
            return kg + "." + mg;
        } else {//kg
            if (kg >= 100) {
                return kg + "." + mg;
            } else {
                if (mg < 10) {
                    return kg + ".0" + mg;
                } else {
                    return kg + "." + mg;
                }
            }
        }
    }

    public void setInitData(double initData) {
        if (initData == -1 || initData == 0) {
            switch (type) {
                case "KG":
                    mDatePick.setData(3, 0);
                    break;
                case "JIN":
                case "LB":
                    mDatePick.setData(6, 0);
                    break;
            }
        } else {
            String data = String.valueOf(initData);
            int big;
            int small;
            if (data.contains(".")) {
                String[] split = data.split("\\.");
                big = Integer.parseInt(split[0]);
                if (!split[1].startsWith("0") && split[1].length() == 1 && type.equals("KG") && big < 100) {
                    small = Integer.parseInt(split[1]) * 10;
                } else {
                    small = Integer.parseInt(split[1]);
                }
            } else {
                big = Integer.parseInt(data);
                small = 0;
            }
            mDatePick.setData(big, small);
        }

    }
}
